<template>
  <el-dialog title="添加分类" :visible="dialogVisible" @close="resetForm">
    <el-form ref="form" :model="formData" :rules="rules">
      <el-form-item label="标题" prop="title">
        <el-input v-model="formData.title" type="text" />
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input v-model="formData.desc" type="textarea" />
      </el-form-item>
      <!-- 上传图片 -->
      <el-form-item label="封面" prop="image">
        <el-upload
          action="http://ye-1308566529.cos.ap-guangzhou.myqcloud.com/bg.png"
          list-type="picture-card"
        >
          <i class="el-icon-plus" />
        </el-upload>
        <el-input v-model="formData.image" placeholder="强制跳过图片上传" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="addClassify">确 定</el-button>
      <el-button @click="resetForm">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addclassifycation, editorclassifycation, getclassifyassignId } from '@/api/classification'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        title: '',
        desc: '',
        image: 'http://ye-1308566529.cos.ap-guangzhou.myqcloud.com/bg.png'
      },
      rules: {
        title: [
          { required: true, message: '请输入分类标题', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '请输入分类描述', trigger: 'blur' }
        ]
      }
    }
  },

  methods: {
    // 新增函数
    async addClassify() {
      // 通过效验
      await this.$refs.form.validate()
      if (this.formData.id) {
      // 调用编辑接口
        await editorclassifycation(this.formData)
      } else {
        // 调用新增接口
        await addclassifycation(this.formData)
      }
      // console.log(res, 62)
      // 刷新
      this.$emit('addEvent')
      // 关闭弹窗
      this.resetForm()
    },
    // 关闭弹窗
    resetForm() {
      // 关闭弹窗
      this.$emit('update:dialogVisible', false)
      // 重置效验--清空表单
      this.$refs.form.resetFields()
    },
    // 编辑回显
    async getEditor(id) {
      const res = await getclassifyassignId(id)
      // console.log(78, res)
      this.formData = res.data
    }
  }
}
</script>

<style scoped>

</style>
